package com.hzy.fivepalace.admin.controller;

import com.hzy.fivepalace.admin.annotation.Login;
import com.hzy.fivepalace.admin.controller.base.BaseController;
import com.hzy.fivepalace.admin.dao.AddAdminUserDto;
import com.hzy.fivepalace.admin.dao.UpdAdminUserDto;
import com.hzy.fivepalace.admin.service.AdminManageService;
import com.hzy.fivepalace.common.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 后台账号管理
 *
 * @author gejian
 */
@RestController
@RequestMapping("/admin/account/manage")
@Api(tags = "后台账号管理-admin账号才能访问")
@ResponseBody
@Login
public class AdminAccountManageController extends BaseController {

    @Autowired
    private AdminManageService adminManageService;


    /**
     * 账号列表
     *
     * @param userName
     * @param page
     * @param size
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "账号列表")
    public TableDataInfo list(
            @ApiParam(value = "检索用:账号") @RequestParam(value = "userName", required = false) String userName,
            @ApiParam(value = "页码", defaultValue = "1", required = true) @RequestParam(defaultValue = "1") Integer page,
            @ApiParam(value = "分页大小", defaultValue = "10", required = true) @RequestParam(defaultValue = "10") Integer size) {

        return adminManageService.getSysUserList(page, size, userName);
    }


    /**
     * 重置密码
     *
     * @param newPassword
     * @param userId
     * @return
     */
    @PostMapping("/resetPasswords")
    @ApiOperation(value = "重置密码")
    public Object resetPasswords(
            @ApiParam(value = "新密码", required = true) @RequestParam(value = "newPassword") @NotBlank String newPassword,
            @ApiParam(value = "用户id", required = true) @RequestParam(value = "userId") @NotNull Integer userId) {
        return adminManageService.resetSysUserPasswords(newPassword, userId);
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @PostMapping("/deleteUser")
    @ApiOperation(value = "删除用户")
    public Object deleteUser(@ApiParam(value = "用户id", required = true) @RequestParam(value = "userId") @NotNull Integer userId) {
        return adminManageService.deleteSysUser(userId);
    }


    /**
     * 新增用户
     *
     * @param dto
     * @return
     */
    @PostMapping("/addUser")
    @ApiOperation(value = "新增用户")
    public Object addUser(AddAdminUserDto dto) {
        return adminManageService.addSysUser(dto);
    }

    /**
     * 修改用户
     *
     * @param dto
     * @return
     */
    @PostMapping("/updUser")
    @ApiOperation(value = "修改用户")
    public Object updUser(UpdAdminUserDto dto) {
        return adminManageService.updSysUser(dto);
    }

}
